/**
 * Copyright (c) 2010 by Kevin Bierhoff.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *    1. Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *    3. Neither the names of the authors nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.googlecode.syper.javasyp.formulae;

import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.IVariableBinding;

/**
 * @author kevin
 * @since Jan 30, 2010
 *
 */
public final class Field implements Name {

	private final Ref obj;
	private final int incarnation;
	/** {@code null} for array.length field, apparently not a real field */
	private IVariableBinding binding;
	private final Local selector;
	/** keep type separately for {@code null} {@link #binding} */

	public Field(Ref obj, IVariableBinding var) {
		this(obj, 0, var);
	}

	/**
	 * Given field must be non-{@code null}.
	 * @param obj
	 * @param incarnation
	 * @param field
	 */
	public Field(Ref obj, int incarnation, IVariableBinding field) {
		this.obj = obj;
		this.incarnation = incarnation;
		this.selector = Local.createFieldVar(field);
		if (field.getDeclaringClass() == null && "length".equals(field.getName()))
			// normalize array.length
			field = null;
		this.binding = field;
	}

	public Field(Ref obj, Local selector) {
		this(obj, selector, 0);
	}

	public Field(Ref obj, Local selector, int incarnation) {
		if (! Local.FIELD_META_TYPE.equals(selector.getMetaType()))
			throw new IllegalArgumentException("need a field variable");
		this.obj = obj;
		this.selector = selector;
		this.incarnation = incarnation;
		this.binding = null;
	}

	public Ref getObj() {
		return obj;
	}

	public int getIncarnation() {
		return incarnation;
	}

	void setBinding(IVariableBinding binding) {
		this.binding = binding;
	}

	// TODO could move into Local (so access through selector field)
	public IVariableBinding getBinding() {
		if (binding == null && ! Local.ARRAY_LENGTH_FIELD_NAME.equals(selector.getId()))
			// make sure this is not called on made-up variables ranging over fields
			// e.g. quantified field variables
			throw new UnsupportedOperationException(
					"binding not available for synthetic field variable: " + selector);
		return binding;
	}

	public String getId() {
		String name = binding == null ? selector.getId() : binding.getName();
		return incarnation == 0 ? name : name + incarnation;
	}

	public boolean isArrayLength() {
		return Local.ARRAY_LENGTH_FIELD_NAME.equals(selector.getId());
	}

	public Local getSelector() {
		return selector;
	}

	@Override
	public ITypeBinding getType() {
		return selector.getType();
	}

	@Override
	public <T> T dispatch(FormulaVisitor<T> visitor) {
		return visitor.visit(this);
	}

	@Override
	public boolean isBoolean() {
		return TypingUtil.isBoolean(getType());
	}

	@Override
	public String toString() {
		return getObj() + "." + getId();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + incarnation;
		result = prime * result + ((obj == null) ? 0 : obj.hashCode());
		result = prime * result
				+ ((selector == null) ? 0 : selector.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Field other = (Field) obj;
		if (incarnation != other.incarnation)
			return false;
		if (this.obj == null) {
			if (other.obj != null)
				return false;
		} else if (!this.obj.equals(other.obj))
			return false;
		if (selector == null) {
			if (other.selector != null)
				return false;
		} else if (!selector.equals(other.selector))
			return false;
		return true;
	}
}
